// pages/certificate/index.js
var {getLoginedUser, fnLoading, fnHideLoading, fnSuccess, fnFail} = require('../../utils/index')
import {domainStatic} from '../../utils/env.js';
import _ from '../../utils/underscore.js';
import {get, fnChoose, lsGetStr, fnUpload, post} from '../../utils/request.js';

Page({
    passWdInput: function (e) {
        this.setData({
            phone: e.detail.value
        })
    },
    obtain: function () {
        let that = this;
        that.setData({
            disabled: true,
            color: "#E8E8E8"
        })
        var phone = this.data.phone;
        var currentTime = this.data.currentTime;
        var warn = null;
        if (phone == '') {
            warn = "号码不能为空";
        } else if (phone.trim().length != 11 || !/^(14[0-9]|13[0-9]|15[0-9]|17[0-9]|18[0-9])\d{8}$$/.test(phone)) {
            warn = "手机号格式不正确";
        } else {
            get({
                path: '/phone-code?phone=' + phone,
            })
                .then(function (res) {
                    console.log(res)
                    fnSuccess("发送成功~")
                })
                .catch(function () {
                    fnFail("发送失败，请稍后重试~")
                })
        }
        if (warn == null) {
            var interval = setInterval(function () {
                currentTime--;
                that.setData({
                    text: currentTime + 's',
                })
                if (currentTime <= 0) {
                    clearInterval(interval);
                    that.setData({
                        text: '重新发送',
                        currentTime: 61,
                        disabled: false,
                        color: '#E8E8E8'
                    })
                }
            }, 1000);
        }
        if (warn != null) {
            wx.showModal({
                title: '提示',
                content: warn
            });
            that.setData({
                disabled: false,
                color: '#E8E8E8'
            })
            return;
        }
    },
    addCode: function (e) {
        this.setData({
            code: e.detail.value
        })
    },
    onSubmit: function () {
        var phone = this.data.phone;
        var code = this.data.code;
        var sessionId = wx.getStorageSync('sessionId');
        // debugger;
        getLoginedUser()
            .then(function (user) {
                return post({
                    path: '/user/update',
                    data: {
                        skey: lsGetStr('skey'),
                        id: user.id,
                        phone: 'phone',
                        code: 'code'
                    }
                })
            })
            .then(function (res) {
                if (res === 1) {
                    fnSuccess('验证成功~')
                    this.setData({
                        couponsBg: false
                    })
                } else {
                    fnFail(res.error || '验证失败，请稍后重试')
                }
            })
            .catch(function(e) {
            fnFail("发送失败"
        )
    })
    },
    /**
     * 页面的初始数据
     */
    data: {
        hasUserInfo: false,
        other: [],
        currentTime: 61,
        text: "获取验证码",
        open: ""
    },
    authSetting(res) {
        let that = this;
        return getApp().authSetting(res)
            .then(function (res) {
                var v = lsGetStr('initValue');
                if (!JSON.parse(res.userInfo).phone) {
                    that.setData({
                        couponsBg: true
                    })
                } else {
                    that.onLoad(that.data.options);
                }
            })
            .catch(function (error) {
                fnFail('注册没有成功，请稍后重试～')
            });
    },

    button: function () {
        let that = this;
        let other = _.filter(this.data.other, function (item) {
            return !!item.selected;
        });
        if (other.length < this.data.other.length) {
            return fnFail('请完成问卷后提交～');
        }
        if (that.data.open == 14184) {
            post({
                path: "/order/apply",
                data: {
                    skey: lsGetStr('skey'),
                    type: 0,
                    shopIds: _.pluck(this.data.other, 'id').join(','),
                    numbers: _.pluck(this.data.other, 'selected').join(',')
                }
            }).then(function (res) {
                fnSuccess("提交成功.....");
                setTimeout(function () {
                    // 软著时跳转至一个新页面
                    wx.redirectTo({
                        url: "../certificate-share/index?type1=11"
                    })
                }, 1000)
            }).catch(function (error) {
                fnFail('请勾选～')
            });
        } else if (that.data.open == 87) {
            post({
                path: "/order/apply",
                data: {
                    skey: lsGetStr('skey'),
                    type: 1,
                    shopIds: _.pluck(this.data.other, 'id').join(','),
                    numbers: _.pluck(this.data.other, 'selected').join(',')
                }
            }).then(function (res) {
                fnSuccess("提交成功.....");
                wx.redirectTo({
                  url: "../certificate-share/index?type1=12"
                })

            })
        } else {
            post({
                path: "/order/apply",
                data: {
                    skey: lsGetStr('skey'),
                    type: 2,
                    shopIds: _.pluck(this.data.other, 'id').join(','),
                    numbers: _.pluck(this.data.other, 'selected').join(',')
                }
            }).then(function (res) {
                fnSuccess("提交成功.....");
                setTimeout(function () {
                    wx.redirectTo({
                      // url: "../my-service/index"
                      url: "../certificate-share/index?type1=13"
                    })
                }, 1000)

            })
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        // console.log(options.type)
        let that = this;
        that.setData({
          options: options.type
        })
        that.data.open = options.type;
        console.log(that.data.open)
        // if (options.type == 14184) {
            get({
                path: "/info/queryWithDr?drType=test-link&sourceId=84&status=1"
            }).then(function (res) {
                wx.setNavigationBarTitle({
                  title: '0元版权申请',
                })
                console.log(res[0]);
                res = res.map(function(item) {
                    item.content = item.content.split(':')
                return item;
            })
                that.setData({
                    options: options,
                    other: res
                })
            })
        // } else if (options.type == 87) {
        //   wx.setNavigationBarTitle({
        //     title: '高新技术企业',
        //   })
        //     get({
        //         path: "/info/queryWithDr?drType=test-link&sourceId=87&status=1"
        //     }).then(function (res) {
        //         res = res.map(function(item ) {
        //             item.content = item.content.split(':')
        //         return item;
        //     })
        //         that.setData({
        //             options: options,
        //             other: res
        //         })
        //     })
        // }
        // else {
        //     get({
        //         path: "/info/queryWithDr?drType=test-link&sourceId=85&status=1"
        //     }).then(function (res) {
        //         res = res.map(function(item ) {
        //             item.content = item.content.split(':')
        //         return item;
        //     })
        //         that.setData({
        //             options: options,
        //             other: res
        //         })
        //     })

        // }
        //获取登录登录用户信息
        getLoginedUser()
            .then(function (user) {
                that.setData({
                    options: options,
                    hasUserInfo: (user && user.id)
                })
            })
    },
    radiochange(e){
        var id = e.detail.value, index = e.currentTarget.dataset.index;
        console.log(id);
        let other = this.data.other;
        this.setData({
            other: [...other.slice(0, index), Object.assign({}, other[index], {selected: id}),...other.slice(index + 1)
    ]
    })
    },

})